package com.liuyjy.knife4j.config;

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import com.liuyjy.knife4j.properties.SwaggerProperties;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import springfox.bean.validators.configuration.BeanValidatorPluginsConfiguration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.ApiSelectorBuilder;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * @Author liuyjy
 * @Date 2022/1/25 19:04
 * @Description: TODO
 **/
@EnableWebMvc
@Configuration
@EnableSwagger2
@EnableKnife4j
@Import(BeanValidatorPluginsConfiguration.class)
@EnableConfigurationProperties({SwaggerProperties.class})
public class Knife4jConfig {

    @Autowired
    private SwaggerProperties properties;

    @Bean
    @ConditionalOnMissingBean
    public Docket docket() {
        Docket docket = new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                //分组名称
                .groupName(properties.getGroupName());
        ApiSelectorBuilder builder = docket.select().apis(RequestHandlerSelectors.any()).paths(PathSelectors.any());
        if (StringUtils.isNotEmpty(properties.getBasePackage())) {
            builder.apis(RequestHandlerSelectors.basePackage(properties.getBasePackage()));
        }
/*        if (properties.isOpenHeader()) {
            List<Parameter> params = new ArrayList<>();
            Parameter header = new ParameterBuilder().name(headerProperties.getAccessToken()).description("用户Token")
                    .modelRef(new ModelRef("string")).parameterType("header")
                    .required(false).defaultValue("").build();
            params.add(header);
            return builder.build().globalOperationParameters(params);
        }*/
        return builder.build();
    }

    @Bean
    @ConditionalOnMissingBean
    @ConditionalOnProperty(name = "common.setting.swagger.registry-handler", havingValue = "true")
    public WebMvcConfig webMvcConfig() {
        return new WebMvcConfig();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                //标题
                .title(properties.getTitle())
                //简介
                .description(properties.getDescription())
                //服务条款
                .termsOfServiceUrl(properties.getServiceUrl())
                //作者个人信息
                .contact(new Contact(properties.getAuthor(), properties.getUrl(), properties.getEmail()))
                //版本
                .version(properties.getVersion())
                .build();
    }

}
